home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / mdosp401.zip / ISPWN.C < prev    next >
C/C++ Source or Header  |  1990-10-02  |  4KB  |  194 lines

  1. /*  Test program which shows how to perform internal task spawns
  2.     test program spawns six functions as internal tasks each of which
  3.     increments a global counter.  The main program prints the counters.
  4.     You should see the numbers change when this program is run.  The
  5.     numbers will change at different rates depending on what the stall
  6.     value for the task incrementing the variable is set to.  Be sure to
  7.     use a large enough value for the RM command prior to loading ISPWN
  8.     so the internal tasks have enough memory for their stacks.  If the
  9.     value is too small, the internal spawn will fail with a code of 2.
  10.    
  11.     This example must be compiled using a large model, both code and
  12.     data must have both segments and offsets.  Be sure that the 
  13.     compiler run time frees up memory at the end of the program so
  14.     MultiDos Plus has some memory to allocate for the internal task's
  15.     stacks.  If your compiler automatically checks for program stack
  16.     integrity when a function is entered, be sure to turn the feature
  17.     off when compiling ispwn.c.
  18.     
  19.     Some C compilers may not like the assignment of a function pointer
  20.     to an int pointer, Quick C produces a warning which can be ignored.
  21. */
  22.  
  23.  
  24. /* ISPWN.C */
  25.   
  26. #include <stdio.h>
  27. #include <dos.h>
  28.  
  29. int c1 = 0;
  30. int c2 = 0;
  31. int c3 = 0;
  32. int c4 = 0;
  33. int c5 = 0;
  34. int c6 = 0;
  35.  
  36. /******************************/
  37. /*  Suspend task for N ticks  */
  38. /******************************/
  39.  
  40. void mdstall(ticks)
  41. int ticks;
  42. {
  43.     union REGS in,out;
  44.  
  45.     in.h.ah = 3; /*  suspend task for interval function code */
  46.     in.x.dx = ticks; /*  number of ticks to suspend for  */
  47.     int86(0x15,&in,&out);
  48. }
  49.  
  50. /**********************/
  51. /*  internal task #1  */
  52. /**********************/
  53.  
  54. void f1()
  55. {
  56.     for (;;)
  57.     {
  58.         mdstall(1);
  59.         c1++;
  60.     }
  61. }
  62.  
  63. /**********************/
  64. /*  internal task #2  */
  65. /**********************/
  66.  
  67. void f2()
  68. {
  69.     for (;;)
  70.     {
  71.         mdstall(4);
  72.         c2++;
  73.     }
  74. }
  75.  
  76. /**********************/
  77. /*  internal task #3  */
  78. /**********************/
  79.  
  80. void f3()
  81. {
  82.     for(;;)
  83.     {
  84.         mdstall(8);
  85.         c3++;
  86.     }
  87. }
  88.  
  89. /**********************/
  90. /*  internal task #4  */
  91. /**********************/
  92.  
  93. void f4()
  94. {
  95.     for (;;)
  96.     {
  97.         mdstall(18);
  98.         c4++;
  99.     }
  100. }
  101.  
  102. /**********************/
  103. /*  internal task #5  */
  104. /**********************/
  105.  
  106. void f5()
  107. {
  108.     for (;;)
  109.     {
  110.         mdstall(36);
  111.         c5++;
  112.     }
  113. }
  114.  
  115. /**********************/
  116. /*  internal task #6  */
  117. /**********************/
  118.  
  119. void f6()
  120. {
  121.     int i;
  122.     
  123.     for (;;) 
  124.     {
  125.         for (i = 0;i < 400; ++i) c6++;
  126.         mdstall(3);
  127.     }
  128. }
  129.  
  130. /*************************************/
  131. /*  SPAWN A TASK IN SAME BOUND UNIT  */
  132. /*************************************/
  133.  
  134. int md_spawn_task(task,task_stack_size)
  135. int *task;
  136. unsigned int task_stack_size;
  137. {
  138.     union REGS in,out;
  139.     int *ptr,ptrl,ptrh;
  140.  
  141.     ptr = task;
  142.     ptrl = *(ptr+0);
  143.     ptrh = *(ptr+1);
  144.     in.h.ah = 07;
  145.     in.x.bx = ptrh;
  146.     in.x.cx = ptrl;
  147.     in.x.dx = task_stack_size;
  148.     int86(0x15,&in,&out); 
  149.     return(int)(out.h.ah);
  150. }
  151.  
  152. /********************/
  153. /* the main program */
  154. /********************/
  155.  
  156. void main()
  157. {
  158.     int err, *ptr;
  159.     
  160.     ptr = (int *)f1;
  161.     err = md_spawn_task(&ptr,25);
  162.     printf("spawn err = %d\n",err);
  163.  
  164.     ptr = (int *)f2;
  165.     err = md_spawn_task(&ptr,25);
  166.     printf("spawn err = %d\n",err);
  167.  
  168.     ptr = (int *)f3;
  169.     err = md_spawn_task(&ptr,25);
  170.     printf("spawn err = %d\n",err);
  171.  
  172.     ptr = (int *)f4;
  173.     err = md_spawn_task(&ptr,25);
  174.     printf("spawn err = %d\n",err);
  175.  
  176.     ptr = (int *)f5;
  177.     err = md_spawn_task(&ptr,25);
  178.     printf("spawn err = %d\n",err);
  179.  
  180.     ptr = (int *)f6;
  181.     err = md_spawn_task(&ptr,25);
  182.     printf("spawn err = %d\n",err);
  183.  
  184.     printf("\r\n\n");
  185.  
  186.     for (;;)
  187.     {
  188.       printf(
  189.          "c1 = %7.d c2 = %7.d c3 = %7.d c4 = %7.d c5 = %7.d c6 = %7.d\r",
  190.             c1, c2, c3, c4, c5, c6);
  191.         mdstall(7);
  192.     }
  193. }
  194.